xquery version "1.0";

import module namespace layout="http://www.example.com/layout" at "xmldb:exist:///db/projdb/modules/layout.xqm";
import module namespace request="http://exist-db.org/xquery/request";



declare option exist:serialize "doctype-public=-//W3C//DTD&#160;XHTML&#160;1.1//EN
                                doctype-system=http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd";
                                
declare function local:per-partij($summery,$aantal){

    element perpartij {
        for $j in distinct-values($summery/entry/partij)
                        let $d := sum($summery/entry[./partij = $j]/aantal)
                        return (element naam{data($j)},element perc{round-half-to-even(100 *($d div $aantal),2)})
                 }
                   
 };                              
declare variable $distinct-parties {distinct-values(data(doc('/db/projdb/lijst_van_sprekers_ge2009.xml')/speakers/speaker/partij))}; 
declare variable $listOfParties {for $party at $position in $distinct-parties return (if ($position != 1) then ' | ' else (), <a href="party.xql?id={util:string-to-binary(xs:string($party))}">{$party}</a>)};
declare variable $partijen  { document('/db/projdb/partijen.xml')/partijen};
declare variable $summ {document('/db/projdb/onderbrekerstotalen.xml')/onderbrekerstotalen};
declare variable $aantal {sum($summ/entry/aantal)};
declare variable $perperc {local:per-partij($summ,$aantal)};

declare variable $onderbrekTovTotaalNaam {<ul>{for $j in $perperc/naam return <li><a href="party.xql?id={util:string-to-binary(xs:string(data($j)))}">{data($j)}</a></li> }</ul>};
declare variable $onderbrekTovTotaalPerc{ <ul>{for $j in $perperc/perc return <li>{data($j)}</li>} </ul>};
declare variable $onderbrekTovTotaalGraf{<img border="1" src="https://chart.googleapis.com/chart?chs=699x200&amp;chco={string-join(for $j in $perperc/naam
                                                                                                             return $partijen/entry[./naam = $j]/kleur,"|")}&amp;chd=t:{ string-join(for $j in $perperc/perc
                                                                                                return $j,",") }&amp;cht=p&amp;chl={string-join(for $j in $perperc/naam
                                                                                                return replace($j,'&amp;','%26'),"|")}"/>};
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="nl">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Activiteit in de plenaire vergaderingen</title>
        <link rel="stylesheet" style="text/css" href="style/core_layout.css"/>
		<script type="text/javascript" src="js/jquery.js"><!-- --></script>
        <script type="text/javascript" src="https://www.google.com/jsapi"><!-- --></script>
        <script type="text/javascript" src="js/activity.js"><!-- --></script>
        <script type="text/javascript" src="js/myScripts.js"><!-- --></script>
        <script type="text/javascript">
            //<![CDATA[
            google.load("visualization", "1", {packages:["corechart"]});
            google.setOnLoadCallback(function() {
                initShowHideButtons();

                activity_setUpdateFunction(function(xml) {
                    activity_loadPartyRankingChart(xml, "partiesActivityChartContainer", 700, 400);
                    
                    activity_loadQuartilePartyPieChart(xml, "ppartyRankingPieChartFirstPartContainer", 'firstPart', 'Kwartiel 1 (top)');
                    activity_loadQuartilePartyPieChart(xml, "ppartyRankingPieChartSecondPartContainer", 'secondPart', 'Kwartiel 2');
                    activity_loadQuartilePartyPieChart(xml, "ppartyRankingPieChartThirdPartContainer", 'thirdPart', 'Kwartiel 3');
                    activity_loadQuartilePartyPieChart(xml, "ppartyRankingPieChartFourthPartContainer", 'fourthPart', 'Kwartiel 4');

                    activity_loadPartyQuartilePieChart(xml, "partyQuartileChartContainerCDenV", "CD&V", true);
                    activity_loadPartyQuartilePieChart(xml, "partyQuartileChartContainerSPA", "sp.a", true);
                    activity_loadPartyQuartilePieChart(xml, "partyQuartileChartContainerVB", "Vlaams Belang", true);
                    activity_loadPartyQuartilePieChart(xml, "partyQuartileChartContainerVLD", "Open VLD", true);
                    activity_loadPartyQuartilePieChart(xml, "partyQuartileChartContainerGroen", "Groen", true);
                    activity_loadPartyQuartilePieChart(xml, "partyQuartileChartContainerNVA", "N-VA", true);
                    activity_loadPartyQuartilePieChart(xml, "partyQuartileChartContainerLDD", "LDD", true);
                    activity_loadPartyQuartilePieChart(xml, "partyQuartileChartContainerUF", "UF", true);
                    activity_loadPartyQuartilePieChart(xml, "partyQuartileChartContainerOnafhankelijke", "Onafhankelijke", true);

                });

                activity_run();
            });
            //]]>
        </script>
        
        
    </head>
    <body>	
          <div class="containerBox">
        {layout:topMenu()}  
        <div class="box" id="titleBox">
            <div class="margin">
                <h1>Partijen</h1>
                {$listOfParties}
            </div>
        </div>

        <div class="box" id="partiesActivityChartBox">
            <div class="margin">
                <h2 class="boxTitle">Activiteitsranking</h2>
                <div class="boxContent">
                    <div id="partiesActivityChartContainer">
                        <!-- Auto generated -->
                    </div>
                </div>
            </div>
        </div>

        <div class="box" id="ppartyRankingPieChartBox">
            <div class="margin">
                <h2 class="boxTitle">Kwartielen</h2>
                <div class="boxDescription">
                <p>De ranking van personen (zie de <a href="ranking.xql">ranking-pagina</a>) kan worden opgedeeld in vier ongeveer even grote groepen (kwartielen genoemd). Het eerste kwartiel bevat de personen die het hoogst in de ranking voorkomen (1, 2, ...), het vierde kwartiel bevat de personen die het laagst in de ranking voorkomen. De grafieken hieronder geven per kwartiel de procentuele aanwezigheid van personen uit de verschillende partijen weer.</p>
                </div>
                <div class="boxContent columnContent">
                    <div class="columnspace twoColumns">
                        <div class="column" id="ppartyRankingPieChartFirstPartContainer" style="height:250px;"><!-- Auto generated --></div>
                        <div class="column" id="ppartyRankingPieChartSecondPartContainer" style="height:250px;"><!-- Auto generated --></div>
                        <br class="clearer" />
                        <div class="column" id="ppartyRankingPieChartThirdPartContainer" style="height:250px;"><!-- Auto generated --></div>
                        <div class="column" id="ppartyRankingPieChartFourthPartContainer" style="height:250px;"><!-- Auto generated --></div>
                        <br class="clearer" />
                    </div>
                </div>
            </div>
        </div>

        <div class="box" id="partyQuartileChartBox">
            <div class="margin">
                <h2 class="boxTitle">Kwartielen per partij</h2>
                <div class="boxDescription">
                <p>De ranking van personen (zie de <a href="ranking.xql">ranking-pagina</a>) kan worden opgedeeld in vier ongeveer even grote groepen (kwartielen genoemd). 
                Het eerste kwartiel bevat de personen die het hoogst in de ranking voorkomen (1, 2, ...), het vierde kwartiel bevat de personen die het laagst in de ranking voorkomen. De grafieken hieronder geven per partij de procentuele aanwezigheid van partijleden in de respectievelijke kwartielen weer.</p>
                </div>
                <div class="boxContent columnContent">
                    <div class="columnspace twoColumns">
                        <div class="column" id="partyQuartileChartContainerCDenV" style="height:250px;"><!-- Auto generated --></div>
                        <div class="column" id="partyQuartileChartContainerSPA" style="height:250px;"><!-- Auto generated --></div>

                        <br class="clearer" />
                        <div class="column" id="partyQuartileChartContainerVB" style="height:250px;"><!-- Auto generated --></div>
                        <div class="column" id="partyQuartileChartContainerVLD" style="height:250px;"><!-- Auto generated --></div>

                        <br class="clearer" />
                        <div class="column" id="partyQuartileChartContainerGroen" style="height:250px;"><!-- Auto generated --></div>
                        <div class="column" id="partyQuartileChartContainerNVA" style="height:250px;"><!-- Auto generated --></div>

                        <br class="clearer" />
                        <div class="column" id="partyQuartileChartContainerLDD" style="height:250px;"><!-- Auto generated --></div>
                        <div class="column" id="partyQuartileChartContainerUF" style="height:250px;"><!-- Auto generated --></div>

                        <br class="clearer" />
                        <div class="column" id="partyQuartileChartContainerOnafhankelijke" style="height:250px;"><!-- Auto generated --></div>
                        <div class="column"><!-- Auto generated --></div>

                        <br class="clearer" />
                    </div>
                </div>
            </div>
        </div>
        
        
        <div class="box" id="onderbrekingsTovTotaalBox">
            <div class="margin">
                <h2 class="boxTitle">Percentage van onderbrekingen per partij t.o.v. het totaal aantal onderbrekingen</h2>
                                <div class="boxDescription">
                <p>Hier wordt weergegeven hoeveel (in percentage) alle partijen onderbreken t.o.v. van het totaal aantal onderbrekingen</p>
                </div>
                <div class="boxContent darkBackground">
                    <div class="columnspace twoColumns darkBackground">
                        <div class="column firstColumn">
                            <h3 class="columnTitle">Naam</h3>
                            {$onderbrekTovTotaalNaam}
                            </div>
                        <div class="column secondColumn">
                            <h3 class="columnTitle">Percentage</h3>
                              {$onderbrekTovTotaalPerc}
                        </div>
                    <br class="clearer"/>
                    </div>
                </div> 
           
                <div class="boxContent" style="margin-top:10px">
                        {$onderbrekTovTotaalGraf}
                </div>
              </div>
          </div>
</div>
    </body>
</html>
